package com.example.projectjava.controller;

import com.example.projectjava.service.LogisticService;
import com.example.projectjava.util.JsonResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@Tag(name = "物流公司接口")
public class LogisticController {
    @Autowired   //此注解的作用：是spring的基本特性，它将项目中运行过程中的JdbcTemplate,自动创建并实例化传入
    JdbcTemplate jdbcTemplate;

    @Autowired
    LogisticService logisticService;

    @ResponseBody
    @GetMapping("/insertLogistic")
    @Operation(summary = "添加物流公司")
    @Parameters({
            @Parameter(name ="dvy_id",description="物流公司id(不允许为空)",in = ParameterIn.QUERY,required = false),
            @Parameter(name ="dvy_name",description="物流公司名称(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="company_home_url",description="物流公司地址",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="query_url",description="网址",in = ParameterIn.QUERY,required = true)

    })
    public JsonResponse insertLogistic(String dvy_id,String dvy_name,String company_home_url,String query_url) {
        JsonResponse jsonResponse = new JsonResponse();
        logisticService.insertLogistic(dvy_id, dvy_name, company_home_url, query_url);    //调用service层的userRegister方法判断用户名手机号是否重复
        jsonResponse.setCode(0);
        jsonResponse.setMsg("插入成功");
        return jsonResponse;
    }

    @ResponseBody
    @GetMapping("/deleteLogistic")
    @Operation(summary = "删除物流公司")
    @Parameters({
            @Parameter(name ="dvy_name",description="物流公司名称(不允许为空)",in = ParameterIn.QUERY,required = false)

    })
    public JsonResponse deleteLogistic(String dvy_name) {
        JsonResponse jsonResponse = new JsonResponse();
        try {
            logisticService.deleteLogistic(dvy_name);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("删除失败:" + e.getMessage());
        }
        return jsonResponse;
    }

    @ResponseBody
    @GetMapping("/queryAllLogistic")
    @Operation(summary = "查询所有物流公司")
    public JsonResponse queryAllLogistic() {
        JsonResponse jsonResponse = new JsonResponse();
        List<Map<String, Object>> logisticList = logisticService.queryAllLogistic();    //调用service层的queryAllUser方法查询所有用户信息
        jsonResponse.setCode(0);
        jsonResponse.setMsg("查询成功");
        jsonResponse.setData(logisticList);
        return jsonResponse;
    }

    @ResponseBody
    @GetMapping("/updateLogistic")
    @Operation(summary = "更新物流公司")
    @Parameters({
            @Parameter(name ="dvy_id",description="物流公司id(不允许为空)",in = ParameterIn.QUERY,required = false),
            @Parameter(name ="dvy_name",description="物流公司名称(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="company_home_url",description="物流公司地址",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="query_url",description="网址",in = ParameterIn.QUERY,required = true)

    })
    public JsonResponse updateLogistic(String dvy_id, String dvy_name, String company_home_url, String query_url) {
        JsonResponse jsonResponse = new JsonResponse();
        try {
            logisticService.updateLogistic(dvy_id, dvy_name, company_home_url, query_url);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("更新失败:" + e.getMessage());
        }
        return jsonResponse;
    }
}
